@echo off
::解决中文乱码
chcp 65001

echo 'MySQL官网下载地址https://dev.mysql.com/downloads/mysql/'
echo '下载完成后解压到想要安装的目录下'
echo '把此脚本移动到解压好的文件夹内，例：D:\mysql\mysql-8.0.37-winx64'


::脚本所在路径，依据此路径去配置环境变量
::echo "%~dp0%"
::获取当前路径
::echo "%cd%"

::增加变量Path赋值当前路径
set Path=%~dp0%
::echo %Path%

echo.
::停止mysql服务
net stop mysql8
mysqld -remove mysql8
echo '删除'

::增加变量file赋值配置文件路径
set file=%Path%my.ini
::echo %file%

:创建初始化文件夹
mkdir init
set initsql=%Path%init\init.sql
echo ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '1qaz'; > %initsql%


:创建文件my.ini，写入[client]
echo [client]> "%file%"
::[client]配置：设置使用的端口
echo port=3306>> "%file%"
::[client]配置：设置使用的字符集
echo default-character-set=utf8>> "%file%"

:写入[mysql]
echo [mysql]>> "%file%"
:: [mysql]配置：设置客户端字符集
echo default-character-set=utf8>> "%file%"

:写入[mysqld]
echo [mysqld]>> "%file%"
::[mysqld]配置：设置端口
echo port=3306>> "%file%"
::[mysqld]配置：设置mysql的安装目录
echo basedir=%Path%>> "%file%"
::[mysqld]配置：设置数据存放目录
echo datadir=%Path%data>> "%file%"
::[mysqld]配置：设置服务器字符集
echo character-set-server=UTF8MB4>> "%file%"
::[mysqld]配置：设置最大连接数
echo max_connect_errors=10>> "%file%"
::[mysqld]配置：设置默认存储引擎
echo default-storage-engine=INNODB>> "%file%"

:解决报错Invalid default value for 'ctime'
::修改数据类型timestamp的默认初始化规则
echo explicit_defaults_for_timestamp=true>> "%file%"
:解决报错Client does not support authentication protocol requested by server; consider upgrading MySQL client
::更改插件认证方式'mysql_native_password'
echo default_authentication_plugin=caching_sha2_password>> "%file%"
echo.
echo.
echo "MySQL配置文件（my.ini）创建完成"


::--initialize-insecure    Create the default database and exit. Create a super user with empty password.
::--user=name    Run mysqld daemon as user.
::--console      Write error output on screen; don't remove the console window on windows.
::删除原有data文件
rd /s /q data
%Path%bin\mysqld --initialize-insecure --user=mysql --console
%Path%bin\mysqld install mysql8
echo '完成'

net start mysql8

::初始化root用户密码1qaz
%Path%bin\mysql.exe -u root < %initsql%
pause